/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BuildData;

import DecisionTree.AttributeSet;
import DecisionTree.DecisionTree;
import DecisionTree.DecisionTreeBuilder;
import DecisionTree.ItemSet;
import DecisionTree.SymbolicAttribute;
import DecisionTree.io.DecisionTreeToDot;
import DecisionTree.io.ItemSetReader;
import DecisionTree.io.ReadWriteDecisionTree;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

/**
 *
 * @author Pham Thanh
 */
public final class NcomTree {
    private AttributeSet attributeSet;
    private DecisionTree tree;
    private ItemSet learningSet;
    public NcomTree() throws IOException, FileNotFoundException, ClassNotFoundException {

        System.out.println("package BuildData; class NcomTree");

        createDecisionTree();
    }
    public void createDecisionTree() throws IOException, FileNotFoundException, ClassNotFoundException {
        System.out.println("Doc du lieu tu file\n");
        this.learningSet = null;
        try{
        //learningSet = ItemSetReader.read(new FileReader("data\\dt\\tree.db"));
        learningSet = ItemSetReader.read("data\\tree\\ncomtreedata.db");
        }catch (IOException e){
            System.out.println("khong doc duoc file");
        }

        this.attributeSet = (AttributeSet) learningSet.attributeSet();
        System.out.println("Doc xong du lieu tu file\n");
        Vector testAttributesVector = new Vector();
        testAttributesVector.add(attributeSet.findByName("charCount"));
        testAttributesVector.add(attributeSet.findByName("numCount"));
        testAttributesVector.add(attributeSet.findByName("chamCount"));
        testAttributesVector.add(attributeSet.findByName("cheoCount"));
        testAttributesVector.add(attributeSet.findByName("ngangCount"));
        testAttributesVector.add(attributeSet.findByName("haiChamCount"));
        testAttributesVector.add(attributeSet.findByName("tknBf2"));
        testAttributesVector.add(attributeSet.findByName("tknBf1"));
//        testAttributesVector.add(attributeSet.findByName("nswStr"));
        testAttributesVector.add(attributeSet.findByName("tknAf1"));
        testAttributesVector.add(attributeSet.findByName("tknAf2"));


        AttributeSet testAttributes = new AttributeSet(testAttributesVector) ;
        SymbolicAttribute goalAttribute =
                (SymbolicAttribute) learningSet.attributeSet().findByName("classNsw");
        System.out.println("Bat dau tao cay\n");
        this.tree = buildTree(learningSet, testAttributes,
                goalAttribute);
        ReadWriteDecisionTree rwd=new ReadWriteDecisionTree();
        rwd.writeToFile("data//tree//ncomtree.db", tree);
        printDot(tree);
        System.out.println("Tao xong cay\n");
    }
     static private void printDot(DecisionTree tree) throws FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("resources\\tree.dot"), "UTF8"));
	System.out.println((new DecisionTreeToDot(tree)).produce());
        String dotFile=(new DecisionTreeToDot(tree)).produce();
        bw.write(dotFile);
        bw.close();
    }
    static private DecisionTree buildTree(ItemSet learningSet,
            AttributeSet testAttributes,
            SymbolicAttribute goalAttribute) {
        DecisionTreeBuilder builder = new DecisionTreeBuilder(learningSet, testAttributes, goalAttribute);

        return builder.build().decisionTree();
    }

}
